﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using MVVMtutorialAdvanced.DomainServices;
using MVVMtutorialAdvanced.Web;
using Riba.MVVMSL;

namespace MVVMtutorialAdvanced
{
	public class Author_VM : ViewModelBase
	{
		public RelayCommand CommandEdit { get; private set; }
		public RelayCommand CommandInsert { get; private set; }
		public RelayCommand CommandDelete { get; private set; }

		private PagedCollectionView _entityList;
		public PagedCollectionView EntityList
		{
			get { return _entityList; }
			set { SetPropertyValue(ref _entityList, value, () => EntityList); }
		}

		private AuthorDTO _selectedEntity;
		public AuthorDTO SelectedEntity
		{
			get { return _selectedEntity; }
			set { SetPropertyValue(ref _selectedEntity, value, () => SelectedEntity); }
		}

		private readonly IDomainServiceBook _domainService;

		public Author_VM(IDomainServiceBook domainService)
		{
			_domainService = domainService;

			CommandEdit = new RelayCommand
				(
					commandData =>
						{
							if (SelectedEntity == null)
								return;

							var args = new LoadChildWindowEventArgs(
								typeof(AuthorDetails), SelectedEntity, null);

							RaiseLoadChildWindow(args);
						}
				);

			CommandInsert = new RelayCommand
				(
					commandData =>
						{
							var author = new AuthorDTO();

							var args = new LoadChildWindowEventArgs(
								typeof(AuthorDetails), author, InsertCallbackFromChildWindow);

							RaiseLoadChildWindow(args);
						}
				);

			CommandDelete = new RelayCommand
				(
					commandData =>
						{
							if (SelectedEntity == null)
								return;

							IsBusy = true;
							domainService.DeleteAuthor(SelectedEntity, DeleteCallback);
						}
				);

			GetItems();
		}

		private void GetItems()
		{
			IsBusy = true;

			_domainService.GetAuthors
				(
					(data, error) =>
						{
							IsBusy = false;

							if (error != null)
							{
								RaiseShowMsgBox(new ShowMsgBoxEventArgs(error.Message, "Fehler in GetItems"));
								return;
							}

							var pagedCollectionView = new PagedCollectionView(data);
							pagedCollectionView.SortDescriptions.Add(
								new SortDescription("LastName", ListSortDirection.Ascending));

							EntityList = pagedCollectionView;

							if (EntityList.Count > 0)
								SelectedEntity = EntityList[0] as AuthorDTO;

							CommandEdit.IsEnabled = true;
							CommandInsert.IsEnabled = true;
							CommandDelete.IsEnabled = true;
						}
				);
		}

		private void InsertCallbackFromChildWindow(ViewModelBase sender, object data)
		{
			((IList<AuthorDTO>)EntityList.SourceCollection).Add(data as AuthorDTO);
			EntityList.Refresh();
			IsBusy = false;
		}

		private void DeleteCallback(Exception error)
		{
			if (error != null)
			{
				RaiseShowMsgBox(new ShowMsgBoxEventArgs(error.Message, "Fehler beim Löschen"));
				return;
			}

			((IList<AuthorDTO>)EntityList.SourceCollection).Remove(SelectedEntity);
			EntityList.Refresh();
			IsBusy = false;
		}
	}
}
